package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisMessageConstant;
import com.itheima.health.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Reference
    private OrderSettingService orderSettingService;

   @Reference
    private MemberService memberService;

    /**
     *
     * @param map 前端传过来的数据
     * @return 返回一个订单结果
     */
    @Override
    @LcnTransaction//控制本地事务的回滚
    public Order add(Map map) {
        //1.验证码校验
            //1.1获取用户输入的验证码并设置过期时间
        String codeFromMap = (String)map.get("validateCode");
        String telephone = (String)map.get("telephone");
        //获取redsi中的验证码
        String redisCode = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER+telephone);
        //验证码是否是失效
        if(StringUtils.isBlank(codeFromMap)){
            //验证码必须填写
            throw new RuntimeException("验证码必须填写");
        }

        if(StringUtils.isBlank(redisCode)){
            //验证码失效
            throw new RuntimeException("验证码失效");
        }

        if(!redisCode.equals(codeFromMap)){
            //验证码不正确
            throw new RuntimeException("验证码不正确");
        }

        //2.是否可以预约
        //查询当天的最大预约人数是否大于已经预约的人数
        String  orderDate = (String)map.get("orderDate");
        int count =   orderSettingService.findOrderSettingByOrderDate(orderDate);
        if(count<=0){
            //不能预约
            throw new RuntimeException("预约人数已满，请选择其他预约时间");
        }

        //3.是否已经预约
        //根据手机号查询会员信息
       Member member =  memberService.findMemberByTelephone(telephone);
        //4.预约成功
        //套餐id
        String sid = (String) map.get("setmealId");
        System.out.println(sid);
        int mid;
        if(member!=null){
            //老会员，判断老会员在当天是否预约该套餐
             mid = member.getId();
            //判断当前用户是否是天已经预约了这个套餐
            int Countexist = baseMapper.findOrderBySetmeatIdAndMemberIdAndOrderDate(Integer.parseInt(sid),mid,orderDate);
            if(Countexist>0){
                //不可以预约
                throw new RuntimeException("不可以重复预约，请择日再约");
            }
        }else{
            //新用户
             member = new Member();
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            member.setIdCard((String)map.get("idCard"));
            member.setPhoneNumber(telephone);
//             //录入会员的时间
//            member.setRegTime(new Date(System.currentTimeMillis()));
            //，新会员存进去，得到新会员的id
             mid = memberService.saveMember(member);
        }
        //更新预约表中的预约人数，根据日期进行更新
       orderSettingService.updateOrderSettingByOrderDate(orderDate);

        //4.预约成功，增加一个订单
        Order order = new Order();
        order.setMemberId(mid);
        order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));
        order.setOrderType((String)map.get("orderType"));
        order.setSetmealId(Integer.parseInt(sid));
        order.setOrderStatus("未到诊");
        save(order);
        return order;
    }

    @Override
    public Map findOrderDetailByOrderId(int id) {
        Map map = baseMapper.findOrderDetailByOrderId(id);
        return   map;
    }
}
